let app = getApp();
const {
  Tool,
  API,
} = global.$util

Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: '',
    activeIndex: 1,
    swiperList: [],
    isAjax: false,
    goodInfo: {},
    goodsSkus: [],
    floorstatus: false, // 是否显示置顶的按钮
    show: false,
    activeIndex: '',
    totalSkuNum: 1,
    unitPrice: '暂无',
    orderType: '',
    payType: 0,
    discountAmount: 0,
    totalAmount: 0,
    orderDetailVOS: [],
    skuId: '',
    skuName: '',
    quantity: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    Tool.isIPhoneX(this)
    // this.ProductFactory = new ProductFactorys(this)
    this.setData({
      id: options.id || 6
    })
    this.getGoodDetail()
  },
  /**
   * 详情数据
   */
  getGoodDetail() {
    if (this.data.isAjax) return
    this.data.isAjax = true
    API.getGoodDetail({
      id: this.data.id
    }).then(res => {
      this.setData({
        goodInfo: {
          ...res.data.data.goods
        },
        goodsSkus: [...res.data.data.goodsSkus]
      })
      let num = this.countTotal(this.data.goodsSkus, 'skuNum')
      this.setData({
        totalSkuNum: num
      })
      this.data.isAjax = false
    }).catch(err => {
      this.data.isAjax = false
    })
  },

  imgCliked(e) { //点击放大图片
    let img = e.currentTarget.dataset.img
    let src = encodeURI(img)
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      // urls: this.data.datas.showContent// 需要预览的图片http链接列表
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // this.getProdList()
  },
  sliderChange(e) { // 轮播切换事件
    this.setData({
      activeIndex: e.detail.current + 1,
      autoplay: true
    })
  },
  swiperImgCliked(e) { //点击放大图片
    let index = e.currentTarget.dataset.index
    let src = encodeURI(this.data.imgUrls[index].smallImg)
    let urls = []
    this.data.imgUrls.forEach((item) => {
      if (item.smallImg) {
        urls.push(item.smallImg)
      }
    })
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: urls // 需要预览的图片http链接列表
    })
  },
  // onChange(event) {
  //   // event.detail 的值为当前选中项的索引
  //   this.setData({
  //     active: event.detail
  //   });
  // },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /*
   * 跳转购物车
   */
  goToCart() {
    Tool.navigateTo('../cart/cart')
  },
  /**
   * 计算对象数组中某个属性合计
   */
  countTotal(arr, keyName) {
    let $total = 0;
    $total = arr.reduce(function (total, currentValue, currentIndex, arr) {
      return currentValue[keyName] ? (total + currentValue[keyName]) : total;
    }, 0);
    return $total;
  },
  /**
   * 弹窗开启/关闭
   */
  onSelectShow() {
    this.setData({
      show: true
    })
  },
  // 弹窗关闭
  onClose() {
    this.setData({
      show: false
    });
  },
  /**
   * sku 切换
   */
  onSelectSku(event) {
    let index = event.currentTarget.dataset.index
    this.setData({
      activeIndex: this.data.activeIndex === index ? '' : index,
      totalSkuNum: this.data.activeIndex === index ? this.countTotal(this.data.goodsSkus, 'skuNum') : this.data.goodsSkus[index].skuNum,
      unitPrice: this.data.activeIndex === index ? "暂无" : this.data.goodsSkus[index].skuPfPrice,
      skuId: this.data.activeIndex === index ? '' : this.data.goodsSkus[index].id,
      skuName: this.data.activeIndex === index ? '' : this.data.goodsSkus[index].skuName
    })
  },
  /**
   * 订单类型切换
   */
  onChangeType(event) {
    this.setData({
      orderType: event.detail,
    });
  },
  /**
   * 订单支付状态
   */
  onChangePay(event) {
    this.setData({
      payType: event.detail,
    });
  },
  /**
   * 购买数
   */
  onChangeQuantity(event) {
    this.setData({
      quantity: event.detail
    });
  },
  /**
   * 加购/生成订单
   */
  onClickButton(event) {
    if (this.data.orderType === '') {
      Tool.showToast("订单类型不能为空");
      return
    }
    if (this.data.skuId === '') {
      Tool.showToast("规格不能为空");
      return
    }
    if (this.data.payType === '' && type === 1) {
      Tool.showToast("支付状态不能为空");
      return
    }
    let type = event.currentTarget.dataset.type
    let params = {
      "shopId": this.data.goodInfo.shopId,
      "orderType": this.data.orderType,
      "payType": type === 0 ? 0 : this.data.payType,
      "discountAmount": 0,
      "totalAmount": 0,
      "orderDetailVOS": [{
        "goodsId": this.data.goodInfo.id,
        "skuId": this.data.skuid,
        "skuName": this.data.skuName,
        "goodsName": this.data.goodInfo.title,
        "quantity": this.data.quantity,
        "unitPrice": this.data.unitPrice,
        "totalPrice": 0
      }]
    }
    if (type === 0) {
      API.addOrder(params).then(() => {
        Tool.showToast("加入购物车成功");
      })
    } else {
      API.addCartGoods(params).then(() => {
        Tool.showToast("生成订单成功");
      })
    }
  }
})